package com.example.supernote.ui.manager

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Article
import androidx.compose.material.icons.filled.OpenWith
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.example.supernote.R
import com.example.supernote.model.manager.AbstractFileComposite
import com.example.supernote.ui.components.UniformButton
import com.example.supernote.ui.components.UniformIcon
import com.example.supernote.utils.dateToFormatString


@Composable
fun PreClassified(
    onConfirm: () -> Unit,
    onCancel: () -> Unit, preClassifiedList: Map<String, MutableList<AbstractFileComposite>>,
    moveFilePath: (path: String, file: AbstractFileComposite) -> Unit,
    originVisible: () -> Unit
) {
    BaseCreateDialog(
        title = stringResource(R.string.manager_auto_classified),
        onDismissRequest = onCancel
    ) {
        PreClassifiedContent(preClassifiedList = preClassifiedList, moveFilePath = moveFilePath,originVisible=originVisible)
        Row {
            Spacer(modifier = Modifier.weight(1f))
            UniformButton(onClick = {
                onConfirm()
                onCancel()
            }, text = stringResource(id = R.string.manager_confirm))
            Spacer(modifier = Modifier.width(5.dp))
            UniformButton(onClick = onCancel, text = stringResource(id = R.string.manager_cancel))
        }
    }
}

@Composable
fun PreClassifiedContent(
    preClassifiedList: Map<String, MutableList<AbstractFileComposite>>,
    moveFilePath: (path: String, file: AbstractFileComposite) -> Unit,
    originVisible: () -> Unit
) {
    val context = LocalContext.current
    LazyColumn(modifier = Modifier.heightIn(max = 200.dp)) {
        items(preClassifiedList.entries.toList()) { entry ->
            Card(
                modifier = Modifier
                    .background(MaterialTheme.colorScheme.background)
                    .padding(8.dp)
                    .border(1.dp, Color.Black, RoundedCornerShape(5.dp))
            ) {
                Column {
                    Text(
                        text = String.format(context.resources.getString(R.string.import_message), entry.key),
                        modifier = Modifier.padding(start = 2.dp, top = 2.dp, bottom = 5.dp),
                        style = MaterialTheme.typography.titleMedium,
                        color = MaterialTheme.colorScheme.onBackground
                    )
                    PreClassifiedItem(
                        modifier = Modifier.padding(bottom = 5.dp),
                        unfiledList = entry.value,
                        moveFilePath = moveFilePath,
                        originVisible = originVisible
                    )
                }
            }
        }
    }
}

@Composable
fun PreClassifiedItem(
    modifier: Modifier,
    unfiledList: MutableList<AbstractFileComposite>,
    moveFilePath: (path: String, file: AbstractFileComposite) -> Unit,
    originVisible: () -> Unit
) {
    var isExpand by remember {
        mutableStateOf(false)
    }
    Column {
        unfiledList.forEach { file ->
            Row(
                modifier = modifier
                    .height(48.dp)
                    .clickable { isExpand = !isExpand },
                verticalAlignment = Alignment.CenterVertically,
            ) {
                UniformIcon(imageVector = Icons.Filled.Article)
                Text(
                    text = file.property!!.name
                )
                Spacer(modifier = Modifier.weight(1f))
                Text(
                    text = dateToFormatString(file.property.lastModified),
                    modifier = Modifier.padding(start = 5.dp),
                )
            }
            if (isExpand) {
                MoveIconExpand(modifier = Modifier, file = file, moveFilePath = moveFilePath, originVisible = originVisible)
            }
        }
    }
}

@Composable
fun MoveIconExpand(
    modifier: Modifier,
    file: AbstractFileComposite,
    moveFilePath: (path: String, file: AbstractFileComposite) -> Unit,
    originVisible: () -> Unit
) {
    var isVisible by remember {
        mutableStateOf(false)
    }
    Row(
        modifier = modifier
            .fillMaxWidth()
            .height(48.dp)
            .padding(end = 5.dp, bottom = 5.dp),
        horizontalArrangement = Arrangement.End
    ) {
        UniformButton(
            onClick = {
                isVisible = true
            },
            imageVector = Icons.Filled.OpenWith,
            text = stringResource(id = R.string.manager_move)
        )
    }
    if (isVisible) {
        MoveFile(
            file = file,
            onConfirm = { path -> moveFilePath(path, file) },
            onCancel = { isVisible = false },
            originVisible = originVisible)
    }
}

@Composable
fun MoveFile(
    file: AbstractFileComposite, onConfirm: (path: String) -> Unit,
    onCancel: () -> Unit,originVisible: () -> Unit
) {
    var movePath by remember {
        mutableStateOf("")
    }
    val context = LocalContext.current
    BaseCreateDialog(title = String.format(context.resources.getString(R.string.move_to), file.property!!.name), onDismissRequest = onCancel) {
        Column(
            modifier = Modifier
                .background(MaterialTheme.colorScheme.background)
                .fillMaxWidth()
                .padding(5.dp)
        ) {
            TextField(
                value = movePath, onValueChange = { movePath = it },
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(bottom = 5.dp),
                placeholder = {
                    Text(
                        text = "input path", color = Color.Gray
                    )
                },
                singleLine = true,
                colors = TextFieldDefaults.colors(unfocusedContainerColor = MaterialTheme.colorScheme.background)
            )
            Row {
                Spacer(modifier = Modifier.weight(1f))
                UniformButton(onClick = {
                    onConfirm(movePath)
                    onCancel()
                    originVisible()
                }, text = stringResource(id = R.string.manager_confirm))
                Spacer(modifier = Modifier.width(5.dp))
                UniformButton(onClick = onCancel, text = stringResource(id = R.string.manager_cancel))
            }
        }
    }
}



